﻿using Company.Core.Helpers;
using Company.Hardware.Base.Entities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Hardware.Base
{
    class DefaultUploader : IUploader
    {
        private readonly string url = "https://localhost:7035";
        /// <summary>
        /// 上传设备解析后的HL7实体数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<T> UploadAsync<T>(HL7 data)
        {
            try
            {
                using(HttpClient client = new HttpClient())
                {
                    var requestUri = url + "/HL7/UploadAsync";
                    var content = new StringContent(JsonConvert.SerializeObject(data),Encoding.UTF8,"application/json");
                    var response = await client.PostAsync(requestUri, content);
                    if(response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch(Exception ex)
            {
                Logs.Error(ex);
            }
            return default(T);
        }
    }
}

